package com.seckillpro.seckill_pro.vo;

import com.seckillpro.seckill_pro.utils.ValidatorUtil;
import com.seckillpro.seckill_pro.validator.IsMobile;
import org.thymeleaf.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Package: com.SecKillpro.SecKill_pro.vo
 * @ClassName: IsMobileValidator
 * @Author: lyx
 * @CreateTime: 2021/3/3 16:44
 * @Description:
 */

public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {

    private boolean required = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {

        //获取它是否是必填的
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {

        if (required) {
            return ValidatorUtil.isMobile(s);
        } else {
            if (StringUtils.isEmpty(s)) {
                return true;
            } else {
                return ValidatorUtil.isMobile(s);
            }
        }
    }
}
